{include file='admin/before_body.phtml' title='后台首页'}
  {include file='admin/header.phtml'}
    <div class="main-container" id="main-container">
      <script type="text/javascript">try { ace.settings.check('main-container' , 'fixed') }catch(e) {  }</script>
      <div class="main-container-inner">
        {include file='admin/sidebar.phtml' navigation='dashboard'}
        <div class="main-content">
{$breadcrumbs=[]}
{$breadcrumbs[] = ['name' => '用户设置', 'icon' => 'icon-home home-icon']}
{$breadcrumbs[] = ['name' => '角色管理', 'isactive' => true]}
{include file='admin/breadcrumb.phtml' breadcrumbs=$breadcrumbs}
          <div class="page-content">
            {html_messages}
            <div class="row">
              <div class="col-sm-12">
                <div class="widget-box">
                  <div class="widget-header">
                    <h4>过滤条件</h4>
                  </div>
                  <div class="widget-body">
                    <div class="widget-main">
                      <form class="form-inline row">
                        <div class="col-sm-3">
                          <input type="text" name="search" value="{$query['search']}" class="form-control" placeholder="需要搜索的关键字">
                        </div>

                        <div class="col-sm-2">
                          <select name="status" class="form-control">
                            <option value=""  {if !is_numeric($query['status']) || !in_array($query['status'], [Role::IS_EFFECTIVE, Role::IS_FORBIDDEN])}selected="selected"{/if}>角色状态</option>
                            <option value="{Role::IS_EFFECTIVE}" {if $query['status'] == Role::IS_EFFECTIVE}selected="selected"{/if}>已启用</option>
                            <option value="{Role::IS_FORBIDDEN}" {if is_numeric($query['status']) && $query['status'] == Role::IS_FORBIDDEN}selected="selected"{/if}>已禁用</option>
                          </select>
                        </div>

                        <div class="col-sm-4">
                          <button type="subbit" class="btn btn-purple btn-sm">
                            查询
                            <i class="icon-search icon-on-right bigger-110"></i>
                          </button>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-xs-12"><div class="space-6"></div></div>

<div class="col-xs-12">
{if $account->checkPermission(['role.edit'])}
  <p>
    <a href="{url('admin/user/role/add')}" class="btn btn-primary btn-sm"><i class="icon-plus align-top bigger-125"></i>新增角色</a>
    <button data-op="efective" class="batch btn btn-success btn-sm">批量启用</button>
    <button data-op="forbidden" class="batch btn btn-danger btn-sm">批量禁用</button>
  </p>
{/if}
  <div class="table-responsive">
    <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid">
      <table id="sample-table-2" class="table table-striped table-bordered table-hover dataTable" aria-describedby="sample-table-2_info">
        <thead>
          <tr>
            <th class="center" width="50"><label><input type="checkbox" class="ace allCheckbox"/><span class="lbl"></span></label></th>
            <th class="center">名称</th>
            <th class="center">状态</th>
            <th class="center">创建时间</th>
            <th class="center">修改时间</th>
            {if $account->checkPermission(['role.edit'])}
            <th class="center" width="160">操作</th>
            {/if}
          </tr>
        </thead>
        <tbody>
          {if empty($rolesList)}
            <tr><td colSpan="6"><p class="center">找不到符合条件的记录<p></td></tr>
          {/if}
          {foreach $rolesList as $role}
            <tr data-id="{$role->role_id}" data-rolename="{$role->rolename}">
              <td class="center">
                <label>
                  <input type="checkbox" class="ace checkboxval" value="{$role->role_id}" />
                  <span class="lbl"></span>
                </label>
              </td>
              <td class="center">{$role->rolename}</td>
              <td class="center">
                {if $role->status == Role::IS_FORBIDDEN}
                <span class="label label-sm label-danger arrowed arrowed-righ">已禁用</span>
                {elseif $role->status == Role::IS_EFFECTIVE}
                <span class="label label-sm label-success arrowed arrowed-righ">已启用</span>
                {else}
                  {$role->status}
                {/if}
              </td>
              <td class="center">{$role->created|date_format:'Y-m-d G:i:s'}</td>
              <td class="center">{$role->updated|date_format:'Y-m-d G:i:s'}</td>
              {if $account->checkPermission(['role.edit'])}
              <td class="center">
                {if $role->status == Role::IS_FORBIDDEN}
                <a class="btn btn-xs btn-success update" data-op="efective">启用</a>
                {elseif $role->status == Role::IS_EFFECTIVE}
                <a class="btn btn-xs btn-danger update" data-op="forbidden">禁用</a>
                {else}
                {/if}
                <a href="{url('admin/user/role/')}{$role->role_id}" class="btn btn-xs btn-info">编辑权限</a>
              </td>
              {/if}
          </tr>
          {/foreach}
        </tbody>
      </table>
      <div class="row">
        <div class="col-sm-6">
        </div>
        <div class="col-sm-6">
          <div class="dataTables_paginate paging_bootstrap">
            {html_pagination page="$page" rows="$rows" count="$count" showinfo="1"
              url="{url('admin/user/role')}" query=$query}
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

            </div>
          </div>
        </div>
      </div>
    </div>
</body>
{include file='admin/after_body.phtml'}
<script>
$('body').delegate('.update', 'click', function(e) {
  e.preventDefault();
  var _this = $(this), tr = _this.closest('tr'), roleid = tr.attr('data-id'),
    rolename = tr.attr('data-rolename'), op = _this.attr('data-op'),
    opname = (op == 'forbidden' ? '禁用' : '启用'), context;
    context = '<p>您正在' + opname + '角色[ ' + rolename +' ]</p>' + (op == 'forbidden' ?
      '禁用角色后，该角色下的用户将失去此角色所有的权限，可能会造成用户丢失部分功能，请谨慎操作':
      '启用角色后，该角色下的用户将用有此角色所有的权限，可能会造成用户拥有敏感权限，请谨慎操作');
    confirm('<div class="message"></div><div class="context">' + context + '</div>', opname + '角色', function(a) {
      var _context = a.find('.context').html();
      var funShowError = function(message) {
        a.find('.message').html([
          '<div class="alert alert-danger" style=" padding: 5px;">',
          '<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i></button>',
          message, '</div>'
        ].join(''));
        a.find('.context').html(_context);
        a.find('.modal-footer .btn-danger').removeClass('disabled');
        a.find('.modal-footer .btn-danger').text('重试');
      };
      var funSuccess = function(message) {
        a.find('.modal-footer .btn-danger').remove();;
        a.find('.context').html(message);
        a.on('hidden.bs.modal', function(e) {
          location.reload();
        });
      };
      var funUpdate = function() {
        a.find('.context').html('<i class="icon-spinner icon-spin orange bigger-125"></i> 正在提交申请，请稍等片刻...');
        $.ajax({
          type: 'POST', dataType: 'json',
          data: { roleid: roleid, op: op },
          url: "{url('admin/user/roleAjax')}",
        }).done(function(result) {
          if (result.code) {
            funShowError(result.msg);
          } else {
            funSuccess(opname + '角色[ ' + rolename + ' ]成功！！');
          }
        }).fail(function() {
          funShowError('服务器开了个小差，请重试！！');
        });
      };
      funUpdate();
    });
});

$('body').delegate('.batch', 'click', function(e) {
  e.preventDefault();
  var _this = $(this), op = _this.attr('data-op'),
    opname = (op == 'forbidden' ? '禁用' : '启用'),
    roleids = [], count, context;
  $('.checkboxval:checked').each(function(){
    roleids.push($(this).val());
  });
  count = roleids.length;
  if (!count) {
    return alert('请选择要' + opname + '的用户！！', '提示');
  }
  context = '<p>您正在批量' + opname + '[ ' + count +  ' ]' + '个角色</p>' + (op == 'forbidden' ?
    '禁用选中角色后，所选角色下的用户将失去此角色所有的权限，可能会造成用户丢失部分功能，请谨慎操作':
    '启用选中角色后，所选角色下的用户将用有此角色所有的权限，可能会造成用户拥有敏感权限，请谨慎操作');
    confirm('<div class="message"></div><div class="context">' + context + '</div>', opname + '角色', function(a) {
    var _context = a.find('.context').html();
    var funShowError = function(message) {
      a.find('.message').html([
        '<div class="alert alert-danger" style=" padding: 5px;">',
        '<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i></button>',
        message, '</div>'
      ].join(''));
      a.find('.context').html(_context);
      a.find('.modal-footer .btn-danger').removeClass('disabled');
      a.find('.modal-footer .btn-danger').text('重试');
    };
    var funSuccess = function(message) {
      a.find('.modal-footer .btn-danger').remove();;
      a.find('.context').html(message);
      a.on('hidden.bs.modal', function(e) {
        location.reload();
      });
    };
    var funUpdate = function() {
      a.find('.context').html('<i class="icon-spinner icon-spin orange bigger-125"></i> 正在提交申请，请稍等片刻...');
      $.ajax({
        type: 'POST', dataType: 'json',
        data: { roleids: roleids, op: op, type: 'batch' },
        url: "{url('admin/user/roleAjax')}",
      }).done(function(result) {
        if (result.code) {
          funShowError(result.msg);
        } else {
          funSuccess('成功' + opname + '[ ' + result.result.affected + ' ]个角色');
        }
      }).fail(function() {
        funShowError('服务器开了个小差，请重试！！');
      });
    };
    funUpdate();
  });
});


</script>
</html>

